package cn.sofwin.blog.service.impl;

import cn.sofwin.blog.dao.UserDao;
import cn.sofwin.blog.entity.Admin;
import cn.sofwin.blog.dao.AdminDao;
import cn.sofwin.blog.entity.User;
import cn.sofwin.blog.service.AdminService;
import cn.sofwin.blog.utils.Dbutils;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.dbutils.DbUtils;
import org.apache.ibatis.session.SqlSession;

/**
 * 服务层
 * @author zx
 * @since 1.0
 *
 */
public class AdminServiceImpl implements AdminService {

    /** 私有的实例对象，用户实现单例模式 */
    private static AdminServiceImpl adminService = new AdminServiceImpl();

    /**
     * 将构造方法设为私有，用于实现单例模式
     */
    private AdminServiceImpl(){
        super();
    }

    /**
     * 对外提供的静态方法，用于获取服务实例
     * @return
     */
    public static AdminService getInstance(){
        return adminService;
    }

    @Override
    public SqlSession getSession() {
        return Dbutils.openSession();
    }

    @Override
    public Admin findByName(String name) {
        return adminService.getSession().getMapper(AdminDao.class).selectByName(name);
    }

    @Override
    public Admin login(String username, String password) {
        Admin admin=adminService.getSession().getMapper(AdminDao.class).selectByName(username);
        //用户名不存在，登录直接失败
        if (admin == null){
            return null;
        }
        //password= DigestUtils.md5Hex(password);

        //密码不匹配，将user置空，代表登录失败
        if (!admin.getPassword().equals(password)){
            admin=null;
        }

        return admin;
    }

}